package com.api.util;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreConnectionPNames;

import com.liuwang.meal.util.SharedPrefer;
import com.liuwang.meal.util.StreamUtils;

import android.content.Context;

 

public class OnlinePay {

	public static String userpay(Context context, String username, String password)
			throws Exception {
		
		 String tableNo = SharedPrefer.ReadTableNo(context)+"";
		 String money=SharedPrefer.ReadSumPrice(context)+"";
		HttpClient client = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(PublicString.userPayUrl);
		List<NameValuePair> parameters = new ArrayList<NameValuePair>();
		parameters.add(new BasicNameValuePair("username", username));
		parameters.add(new BasicNameValuePair("password", password));
		parameters.add(new BasicNameValuePair("money", money));
		parameters.add(new BasicNameValuePair("tableNo", tableNo));
		 
		client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,
				"UTF-8");
		httpPost.setEntity(entity);
		
		HttpResponse response = client.execute(httpPost);
		int code = response.getStatusLine().getStatusCode();
	  
		System.out.println("发送地址信息得到的返回吗" + code);
	 
		if (code == 200) {
			InputStream is = response.getEntity().getContent();
            String result = StreamUtils.readStream(is);
			System.out.println("发送地址信息得到的结果" + result);
			return result;
		} else {
			return null;
		}
	}
	
	public static String adminPay(Context context, String username, String password)
			throws Exception {
		
		 String tableNo = SharedPrefer.ReadTableNo(context)+"";
		 String money=SharedPrefer.ReadSumPrice(context)+"";
		HttpClient client = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(PublicString.adminPayUrl);
		List<NameValuePair> parameters = new ArrayList<NameValuePair>();
		parameters.add(new BasicNameValuePair("username", username));
		parameters.add(new BasicNameValuePair("password", password));	
		parameters.add(new BasicNameValuePair("tableNo", tableNo));
		 
		client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 5000);
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,
				"UTF-8");
		httpPost.setEntity(entity);
		
		HttpResponse response = client.execute(httpPost);
		int code = response.getStatusLine().getStatusCode();
	  
		System.out.println("发送地址信息得到的返回吗" + code);
	 
		if (code == 200) {
			InputStream is = response.getEntity().getContent();
            String result = StreamUtils.readStream(is);
			System.out.println("发送地址信息得到的结果" + result);
			return result;
		} else {
			return null;
		}
	}
}
